#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/shm.h>
#include "../macro.h"
#include <string.h>

using std::cout;
using std::endl;

void wr()
{
    int shmid = shmget(1000, 4096, IPC_CREAT | 0600);
    ERR_CHECK(shmid, -1, "shmget");
    char *p = (char *)shmat(shmid, NULL, 0);
    ERR_CHECK(p, (char *)-1, "shmat");
    strcpy(p, "hello");
    shmdt(p);
}

void re()
{
    int shmid = shmget(1000, 4096, 0600);
    ERR_CHECK(shmid, -1, "shmget");
    char *p = (char *)shmat(shmid, NULL, 0);
    ERR_CHECK(p, (char *)-1, "shmat")
    printf("%s\n", p);
    shmdt(p);
    // shmctl();
    struct shmid_ds shm{0};
    shmctl(shmid, IPC_RMID, &shm);
    printf("cpid=%d, size=%ld\n", shm.shm_cpid, shm.shm_segsz);
}

int main(int args, char *argv[])
{
    // wr();
    re();
    return 0;
}